/**
 * 用户访问管理
 */
import { Op } from 'sequelize'

class AdminVisitorController extends BasicController {

    /**
     * 用户访问
     * @return {[type]} [description]
     */
    async actionIndex() {
        let { page, status, search, building_name, start_time, end_time,lock_door_type } = $_POST;
        let number = page && page.number !== undefined ? page.number : 1;
        let size = page && page.size !== undefined ? page.size : 20;
        let where = {}
        if (status) {
            where['status'] = status;
        }
        if (lock_door_type) {
            where['lock_door_type'] = lock_door_type;
        }
        if (start_time) {
            where['created_time'] = {
                [Op.gte]: start_time
            };
        }
        if (end_time) {
            if (where['created_time']) {
                where = {
                    [Op.and]: [
                        where,
                        {
                            created_time: {
                                [Op.lte]: end_time
                            }
                        }
                    ]
                }
            } else {
                where['created_time'] = {
                    [Op.lte]: end_time
                }
            }

        }
        let joinWhere = {};
        if (search) {
            joinWhere['user'] = {
                [Op.or]: [{
                        real_name: {
                            [Op.like]: '%' + search + '%'
                        }
                    },
                    {
                        id_card: {
                            [Op.like]: '%' + search + '%'
                        }
                    }
                ]

            };
        }
        if (building_name) {
            joinWhere['building'] = {
                name: {
                    [Op.like]: '%' + building_name + '%'
                }
            };
        }
        console.log(where)
        return await this.modules('Visitor').where(where).joinWith(['user', 'building', 'company'], joinWhere).page(number, size).order([
            ['id', 'DESC']
        ]).select();
    }

    /**
     * 预警状态修改
     * @return {[type]} [description]
     */
    async actionUpdate() {
        let { id, status, note } = $_POST;
        let data = {};
        if (status) {
            data['status'] = status;
        }
        if (note) {
            data['note'] = note;
        }


        return await this.modules('Visitor').where({ id }).update(data)
    }

    /**
     * 关注人员列表
     * @return {[type]} [description]
     */
    async actionFocusIndex() {
        let { page, search } = $_POST;
        let number = page && page.number !== undefined ? page.number : 1;
        let size = page && page.size !== undefined ? page.size : 20;
        let where = {};
        if (search) {
            where = {
                [Op.or]: [{
                        real_name: {
                            [Op.like]: '%' + search + '%'
                        }
                    },
                    {
                        id_card: {
                            [Op.like]: '%' + search + '%'
                        }
                    }
                ]
            }
        }

        return await this.modules('Visitor.Focus').where(where).page(number, size).order([
            ['id', 'DESC']
        ]).select();
    }

    /**
     * 关注人员详情
     * @return {[type]} [description]
     */
    async actionFocusView() {
        let id = $_POST['id'];
        let data = await this.modules('Visitor.Focus').where({ id }).first();
        if (data) {
            return data;
        } else {
            throw new Error('数据不存在');
        }
    }

    /**
     * 新增关注人员
     * @return {[type]} [description]
     */
    async actionFocusCreate() {
        let { id_card } = $_POST;
        let check = await this.modules('Visitor.Focus').where({ id_card }).first();
        if (check) {
            throw new Error('身份证号:'+check.id_card+',人员已存在');
        }
        let data = await this.modules('Visitor.Focus').insert($_POST);
        if (data) {
            return data;
        } else {
            throw new Error('保存失败');
        }
    }
/**
     * 新增关注人员(批量)
     * @return {[type]} [description]
     */
    async actionFocusBulkCreate() {
        let entitys= $_POST;
        let id_cards=entitys.map(p=>p.id_card);
        let check = await this.modules('Visitor.Focus').where({ id_card:id_cards }).first();
        if (check) {
            throw new Error('身份证号:'+check.id_card+',人员已存在');
        }
        let data = await this.modules('Visitor.Focus').bulkCreate($_POST);
        if (data) {
            return data;
        } else {
            throw new Error('保存失败');
        }
    }
    /**
     * 编辑关注人员
     * @return {[type]} [description]
     */
    async actionFocusUpdate() {
        let { id, id_card } = $_POST;
        let check = await this.modules('Visitor.Focus').where({ id_card }).first();
        if (check && check.id != id) {
            throw new Error('人员已存在');
        }
        let data = await this.modules('Visitor.Focus').where({ id }).update($_POST);
        if (data) {
            return data;
        } else {
            throw new Error('保存失败');
        }
    }

    /**
     * 删除关注人员
     * @return {[type]} [description]
     */
    async actionFocusDelete() {
        let id = $_POST['id'];
        let res = await this.modules('Visitor.Focus').where({ id }).delete();
        if (res) {
            return true;
        } else {
            throw new Error('删除失败');
        }
    }

}

export default AdminVisitorController;